home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-08 | 7.1 KB | 345 lines | [TEXT/MPCC] |
- /*
- File: PictureWindow.cp
-
- Contains: A window designed to hold a PICT, no scroll bars...
- Written by: Dave Mark
- Copyright: ©1995 by Dave Mark, all rights reserved.
- */
-
-
- const short kPictureWindowTemplateID = 1028;
- const short kDefaultPICTResID = 1025;
-
-
- #include "PictureWindow.h"
- #include <ToolUtils.h>
-
-
- MenuHandle TPictureWindow::fgMenu;
- unsigned long TPictureWindow::fgWindowTitleCount = 0;
-
- extern TMenuBar* gMenuBar;
-
- // methods
-
- TPictureWindow::TPictureWindow()
- {
- fDraggedPicHandle = nil;
-
- TPictureWindow::fgWindowTitleCount++; // Starts out as zero but we want “Picture Window 1”
- this->CreateWindow(); // kNormalWindow is default. Method inherited from TWindow
- // TWindow::CreateWindow() calls our MakeNewWindow().
- }
-
-
- TPictureWindow::~TPictureWindow()
- {
- // No particular cleanup in this version, since our real purpose was
- // to demonstrate the Drag Manager, not to build a picture handling app.
- }
-
-
- WindowRef
- TPictureWindow::MakeNewWindow( WindowRef behindWindow )
- {
- WindowRef aWindow;
- Str255 titleString;
- GrafPtr savedPort;
-
- GetPort(&savedPort);
-
- aWindow = GetNewColorOrBlackAndWhiteWindow( kPictureWindowTemplateID,
- nil, behindWindow );
-
- if (aWindow)
- {
- GetWTitle(aWindow,titleString);
- if (StrLength(titleString) != 0)
- {
- Str255 numberString;
-
- NumToString( fgWindowTitleCount, numberString );
- BlockMove(&numberString[1],&titleString[titleString[0]+1],numberString[0]);
- titleString[0] += numberString[0];
- }
- SetWTitle(aWindow,titleString);
-
- SetPortWindowPort(aWindow);
-
- ShowWindow(aWindow);
- }
- SetPort(savedPort);
-
- return aWindow;
- }
-
-
- void
- TPictureWindow::Draw(void)
- {
- PicHandle pic;
- Rect r;
-
- r = GetWindowPort(fWindow)->portRect;
- EraseRect( &r );
-
- if ( fDraggedPicHandle == nil )
- pic = this->LoadDefaultPicture();
- else
- pic = fDraggedPicHandle;
-
- this->CenterPict( pic, &r );
- DrawPicture( pic, &r );
- }
-
-
- void
- TPictureWindow::Activate( Boolean activating )
- {
- if ( activating )
- {
- InsertMenu( fgMenu, 0 );
- gMenuBar->Invalidate();
- }
- else
- DeleteMenu( mPicture );
- }
-
-
- void
- TPictureWindow::Click( EventRecord * )
- {
- this->Select();
- }
-
-
- void
- TPictureWindow::ClickAndDrag( EventRecord *eventPtr )
- {
- OSErr err;
- DragReference dragRef;
- RgnHandle dragRegion, tempRgn;
- Rect itemBounds;
- Handle flavorDataHandle;
-
- err = NewDrag( &dragRef );
- if ( err != noErr )
- return; // Should return err, but needs a change to TWindow::ClickAndDrag return type
-
- if ( fDraggedPicHandle == nil )
- flavorDataHandle = (Handle)this->LoadDefaultPicture();
- else
- flavorDataHandle = (Handle)fDraggedPicHandle;
-
- HLock( flavorDataHandle );
-
- err = AddDragItemFlavor( dragRef,
- (ItemReference)fWindow,// no particular reason, could use any number here
- (FlavorType) 'PICT',
- (Ptr)*flavorDataHandle,
- GetHandleSize( (Handle)flavorDataHandle ),
- (FlavorFlags)0 );
-
- HUnlock( flavorDataHandle );
-
- if ( err != noErr )
- {
- DisposeDrag( dragRef );
- return; // Should return err, but needs a change to TWindow::ClickAndDrag return type
- }
-
- itemBounds = this->GetContentsBounds();
-
- err = SetDragItemBounds( dragRef, (ItemReference)fWindow, &itemBounds );
- if ( err != noErr )
- {
- DisposeDrag( dragRef );
- return; // Should return err, but needs a change to TWindow::ClickAndDrag return type
- }
-
- dragRegion = NewRgn();
- RectRgn( dragRegion, &itemBounds );
- tempRgn = NewRgn();
- CopyRgn( dragRegion, tempRgn );
- InsetRgn( tempRgn, 1, 1 );
- DiffRgn( dragRegion, tempRgn, dragRegion );
- DisposeRgn( tempRgn );
-
- err = TrackDrag( dragRef, eventPtr, dragRegion );
- DisposeRgn( dragRegion );
- DisposeDrag( dragRef );
- return; // Should return err, but needs a change to TWindow::ClickAndDrag return type
- }
-
-
- OSErr
- TPictureWindow::DragEnterWindow( DragReference dragRef )
- {
- fCanAcceptDrag = IsPictFlavorAvailable( dragRef );
- fIsWindowHighlighted = false;
-
- if ( fCanAcceptDrag )
- return noErr;
- else
- return dragNotAcceptedErr;
- }
-
-
- OSErr
- TPictureWindow::DragInWindow( DragReference dragRef )
- {
- DragAttributes attributes;
- RgnHandle tempRgn;
-
- GetDragAttributes( dragRef, &attributes );
-
- if ( (! fCanAcceptDrag) || (! (attributes & dragHasLeftSenderWindow))
- || (attributes & dragInsideSenderWindow) )
- return dragNotAcceptedErr;
-
- if ( this->IsMouseInContentRgn( dragRef ) )
- {
- if ( ! fIsWindowHighlighted )
- {
- tempRgn = NewRgn();
- RectRgn( tempRgn, &GetWindowPort(fWindow)->portRect );
-
- if ( ShowDragHilite( dragRef, tempRgn, true ) == noErr )
- fIsWindowHighlighted = true;
-
- DisposeRgn(tempRgn);
- }
- }
-
- return noErr;
- }
-
-
- OSErr
- TPictureWindow::DragLeaveWindow( DragReference dragRef )
- {
- if ( fIsWindowHighlighted )
- HideDragHilite( dragRef );
-
- fIsWindowHighlighted = false;
- fCanAcceptDrag = false;
-
- return noErr;
- }
-
-
- OSErr
- TPictureWindow::HandleDrop( DragReference dragRef )
- {
- OSErr err;
- Size dataSize;
- ItemReference item;
- FlavorFlags flags;
- DragAttributes attributes;
-
- GetDragAttributes( dragRef, &attributes );
-
- if ( attributes & dragInsideSenderWindow )
- return dragNotAcceptedErr;
-
- err = GetDragItemReferenceNumber( dragRef, 1, &item );
- if ( err == noErr )
- err = GetFlavorFlags( dragRef, item, 'PICT', &flags );
-
- if ( err == noErr )
- {
- err = GetFlavorDataSize( dragRef, item, 'PICT', &dataSize);
- if (err == noErr )
- {
- fDraggedPicHandle = (PicHandle)TempNewHandle( dataSize, &err );
-
- if ( fDraggedPicHandle == nil )
- fDraggedPicHandle = (PicHandle)NewHandle( dataSize );
-
- if ( fDraggedPicHandle == nil )
- err = dragNotAcceptedErr;
- else
- {
- HLock( (Handle)fDraggedPicHandle );
- err = GetFlavorData( dragRef, item, 'PICT',
- *fDraggedPicHandle, &dataSize, 0L );
- HUnlock( (Handle)fDraggedPicHandle );
-
- if ( err != noErr)
- {
- err = dragNotAcceptedErr;
- DisposeHandle( (Handle)fDraggedPicHandle );
- fDraggedPicHandle = nil;
- }
- else
- {
- SetPortWindowPort( fWindow );
- InvalRect( &(GetWindowPort(fWindow)->portRect) );
- }
- }
- }
- }
-
- return( err );
- }
-
-
- PicHandle
- TPictureWindow::LoadDefaultPicture()
- {
- PicHandle pic;
-
- pic = GetPicture( kDefaultPICTResID );
-
- if ( pic == nil )
- {
- DebugStr( (StringPtr) "\pCould not load PICT resource!" );
- return (PicHandle)nil;
- }
- else
- return( pic );
- }
-
-
- void
- TPictureWindow::CenterPict( PicHandle picture, Rect *destRectPtr )
- {
- Rect windRect, pictRect;
-
- windRect = *destRectPtr;
- pictRect = (**( picture )).picFrame;
- OffsetRect( &pictRect, windRect.left - pictRect.left,
- windRect.top - pictRect.top);
- OffsetRect( &pictRect,(windRect.right - pictRect.right)/2,
- (windRect.bottom - pictRect.bottom)/2);
- *destRectPtr = pictRect;
- }
-
-
- Boolean
- TPictureWindow::IsPictFlavorAvailable( DragReference dragRef )
- {
- unsigned short numItems;
- FlavorFlags flags;
- OSErr err;
- ItemReference item;
-
- CountDragItems( dragRef, &numItems );
-
- if ( numItems < 1 )
- return( false );
-
- err = GetDragItemReferenceNumber( dragRef, 1, &item );
- if ( err == noErr )
- err = GetFlavorFlags( dragRef, item, 'PICT', &flags );
-
- return( err == noErr );
- }
-
-
- void
- TPictureWindow::SetUpStaticMenu( void )
- {
- TPictureWindow::fgMenu = gMenuBar->GetMenuFromCMNU( mPicture );
- }
-